//
//  CCInfoViewController.m
//  coninbase_data
//
//  Created by 2.5 on 2021/9/8.
//  Copyright © 2021 ChinaRainbow-iOS. All rights reserved.
//

#import "CCInfoViewController.h"

@interface CCInfoViewController ()<UITextViewDelegate>
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UITextField *nametf;
@property (nonatomic,strong)UITextField *sextf;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UIButton *editButton;
@property (nonatomic,strong)UIScrollView *scrollView;
@end

@implementation CCInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customeCCUI];
}
-(void)customeCCUI
{
    self.editButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    [self.editButton setTitle:@"eidt now" forState:UIControlStateNormal];
    [self.editButton setTitle:@"finish" forState:UIControlStateSelected];
    [self.editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.editButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
    [self.editButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.editButton];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight)];
    
    [self.view addSubview:self.scrollView];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.ccwidth/2.0-40, 80, 80, 80)];
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 40;
    [self.scrollView addSubview:self.imageView];
    self.imageView.image = [UIImage imageNamed:@"head_default44"];
    
    self.nametf = [[UITextField alloc] initWithFrame:CGRectMake(self.view.ccwidth/2.0-60, 180, 120, 40)];
    self.nametf.enabled = NO;
    self.nametf.placeholder = @"edit your name";
    self.nametf.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"name"]);
    self.nametf.textAlignment = NSTextAlignmentCenter;
    self.nametf.layer.borderWidth = 1;
    self.nametf.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.scrollView addSubview:self.nametf];
    
    self.sextf = [[UITextField alloc] initWithFrame:CGRectMake(self.view.ccwidth/2.0-60, 240, 120, 40)];
    self.sextf.enabled = NO;
    self.sextf.placeholder = @"edit your sex info";
    self.sextf.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"sex"]);
    self.sextf.textAlignment = NSTextAlignmentCenter;
    self.sextf.layer.borderWidth = 1;
    self.sextf.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.scrollView addSubview:self.sextf];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(30, 290, self.view.ccwidth-60, 100)];
    self.textView.editable = NO;
    self.textView.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"info"]);
    
    self.textView.delegate = self;
    self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView.layer.borderWidth = 1;
    [self.scrollView addSubview:self.textView];
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"edit your info"]){
        textView.text = @"";
    }
}
-(void)editAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if(self.editButton.selected){
        self.nametf.enabled = YES;
        self.textView.editable = YES;
        self.sextf.enabled = YES;
        self.imageView.userInteractionEnabled = YES;
    }else{
        self.nametf.enabled = NO;
        self.textView.editable = NO;
        self.sextf.enabled = NO;
        self.imageView.userInteractionEnabled = NO;
        [[NSUserDefaults standardUserDefaults] setObject:self.nametf.text forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:self.sextf.text forKey:@"sex"];
        [[NSUserDefaults standardUserDefaults] setObject:self.textView.text forKey:@"info"];
    }
    
}
@end
